package com.cn.yugan.Utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.cn.yugan.mobile.R;

import java.io.ByteArrayOutputStream;


/**
 * Created by Rover on 2016/10/9 .
 * Email 357319517@qq.com
 */
public class ImgUtils {

    //byte[]转Bitmap
    public static Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    //byte[]转drawable
    public static Drawable Bytes2Drawable(byte[] b) {
        if (b.length != 0) {
            Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
            Drawable drawable = new BitmapDrawable(Resources.getSystem(), bmp);
            return drawable;
        } else {
            return null;
        }
    }

    //byte[]转drawable
    public static byte[] Drawable2Bytes(Drawable d) {
        BitmapDrawable bd = (BitmapDrawable) d;
        Bitmap bmp = bd.getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
    

}
